<template>
  <div>
    <div class="row">
        <div class="column">
          <h2 class="category">{{ name }}</h2>
          <div class="products" v-if="product">
            <div v-for="(item, index) in product"
            :key="index"
            class="product-item">
              <a :href="`/indProduct/${item.id}`" target="_blank">
                <div>
                  <img :src="item.url" alt="">
                  <div class="details">
                    <span class="name">{{ item.name }}</span>
                    <span class="price">￥{{ item.price }}</span>
                  </div>
                </div>
              </a>
            </div>
          </div>
          <h2 v-if="product.length === 0">暂无该分类商品</h2>
        </div>
      </div>
  </div>
</template>

<script>
export default {
    data() {
        return {
            product: [],
            name: ''
        }
    },
    created() {
        this.name = this.$route.params.name
        this.getProduct()
    },
    methods: {
        async getProduct() {
            const { data: res } = await this.$axios.get('/product/category/' + this.name)
            console.log('商品数据：' + res)
            if (res.code !== 200) {
                return this.$message.error('查询该分类商品失败,请重新查询')
            }
            this.product = res.data.product
        }
    }
}
</script>

<style>
  .row {
    display: flex;
    justify-content: center;
  }

  .column {
    margin: 20px;
  }

  .category {
    text-align: center;
  }

  .products {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .product-item {
    margin: 10px;
    /* Calculate width: subtract the total margin space (left and right)
      from the 1/4th of the parent container. */
    /* You may have to adjust the values if you're providing padding or border. */
    width: calc(20% - 10px);
    border: 1px solid #ccc;
    border-radius: 5px;
    padding: 10px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
  }

  .product-item:hover {
    box-shadow: 0 0 10px rgb(221, 3, 3);
  }

  .product-item a {
    text-decoration: none;
    color: inherit;
  }

  .product-item img {
    width: 100%;
    height: auto;
    border-radius: 5px;
  }

  .details {
    margin-top: 10px;
    text-align: center;
    font-size: 25px;
  }

  .name {
    font-weight: bold;
  }

  .price {
    color: #ff0000;
  }
</style>
